package com.ymatou.iapi.trading.operate.testcase;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import org.apache.http.client.ClientProtocolException;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.trading.operate.parameter.OrderItemVo;
import com.ymatou.iapi.trading.operate.parameter.PlaceOrderResp;
import com.ymatou.iapi.trading.operate.parameter.PlaceOrderService;
import com.ymatou.iapi.trading.operate.parameter.enums.LogisticsTypeEnum;
import com.ymatou.iapi.trading.operate.parameter.req.ChangeEcommerceFlagReq;
import com.ymatou.iapi.trading.operate.parameter.req.DeliveryTimeoutReq;
import com.ymatou.iapi.trading.operate.parameter.req.PlaceOrderReq;
import com.ymatou.iapi.trading.operate.service.ChangeEcommerceFlagCall;
import com.ymatou.iapi.trading.operate.service.DeliveryTimeoutCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.OrderCallService;
import com.ymttest.database.sqlwapper.DefaultWapper;
/**
*电商件已接单48小时，消息提醒
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_DeliveryTimeout {
	private static DeliveryTimeoutReq deliveryTimeoutReq;
	private static DeliveryTimeoutCall deliveryTimeoutCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("电商件已接单48小时,消息提醒");
	}

	@Before
	public void caseUp() {
		deliveryTimeoutReq = new DeliveryTimeoutReq();
		deliveryTimeoutCall = new DeliveryTimeoutCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(deliveryTimeoutCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	private static void changeEcommerceFlagCall(int orderId, int ecommerceFlag)
			throws ClientProtocolException, IOException {
		ChangeEcommerceFlagReq changeEcommerceFlagReq = new ChangeEcommerceFlagReq();
		ChangeEcommerceFlagCall changeEcommerceFlagCall = new ChangeEcommerceFlagCall();

		changeEcommerceFlagReq.setOrderId(Long.valueOf(orderId));
		changeEcommerceFlagReq.setEcommerceFlag(ecommerceFlag);
		changeEcommerceFlagReq.setAppId("autotest");
		changeEcommerceFlagReq.setBillCode("autotest111");

		changeEcommerceFlagCall.setData(changeEcommerceFlagReq);
		changeEcommerceFlagCall.callService();
	}

	/**
	 * 订单为电商件,物流为贝海直邮
	 * @return PlaceOrderResp
	 */
	public PlaceOrderResp placeOrder(String receiveName) {

		PlaceOrderReq placeorderBean = PlaceOrderService.getDefaultOrderReq();

		OrderItemVo OrderItemVo = placeorderBean.getOrderGroups().get(0)
				.getOrders().get(0).getOrderItems().get(0);

		placeorderBean.getOrderGroups().get(0).getOrders().get(0)
				.setEcommerceFlag(true);

		placeorderBean.getOrderGroups().get(0).getOrders().get(0)
				.setReceiveName(receiveName);

		OrderItemVo.setLogisticsType(LogisticsTypeEnum.SailProtect);

		return PlaceOrderService.callPlaceOrderService(placeorderBean);

	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_DeliveryTimeout_001() {
		Logger.start(true, "电商件,物流为贝海直邮,用户没上传过身份证号,推送消息提醒");

		try {
			PlaceOrderResp placeOrderResp = placeOrder("李四四");

			int sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			int userId = placeOrderResp.getUserId();
			int tradingId = placeOrderResp.getTradingId();
			int orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			BigDecimal payAmount = placeOrderResp.getPayAmount();

			new OrderCallService()
					.orderPayRequest(userId, payAmount, tradingId);

			new OrderCallService().acceptOrder(orderId, sellerId);

			deliveryTimeoutReq.setOrderId(orderId);

			deliveryTimeoutCall.setData(deliveryTimeoutReq);

			deliveryTimeoutCall.callService();

			Logger.verifyEquals("ok", deliveryTimeoutCall.getReturnData(),
					"验证Code");

			//数据库验证

			DefaultWapper database = new DefaultWapper();

			// 订单扩展表
			List<Map> ymtOrdersExt = database.selectOrderExtByOderId(orderId);

			Logger.verifyEquals(false,
					ymtOrdersExt.get(0).get("bHaveUploadedIdCard"), "验证身份证状态");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_DeliveryTimeout_002() {
		Logger.start(true, "电商件,物流为贝海直邮,用户上传过身份证号,不推送消息提醒");

		try {
			PlaceOrderResp placeOrderResp = placeOrder("李四");

			int sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			int userId = placeOrderResp.getUserId();
			int tradingId = placeOrderResp.getTradingId();
			int orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			BigDecimal payAmount = placeOrderResp.getPayAmount();

			new OrderCallService()
					.orderPayRequest(userId, payAmount, tradingId);

			new OrderCallService().acceptOrder(orderId, sellerId);

			deliveryTimeoutReq.setOrderId(orderId);

			deliveryTimeoutCall.setData(deliveryTimeoutReq);

			deliveryTimeoutCall.callService();

			Logger.verifyEquals("ok", deliveryTimeoutCall.getReturnData(),
					"验证Code");

			//数据库验证

			DefaultWapper database = new DefaultWapper();

			// 订单扩展表
			List<Map> ymtOrdersExt = database.selectOrderExtByOderId(orderId);

			Logger.verifyEquals(true,
					ymtOrdersExt.get(0).get("bHaveUploadedIdCard"), "验证身份证状态");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_DeliveryTimeout_003() {
		Logger.start(true, "电商件,物流为贝海直邮,用户没上传过身份证号,接到过贝海通知实际为个人件,不推送消息提醒");
		try {

			PlaceOrderResp placeOrderResp = placeOrder("李四四");

			int sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			int userId = placeOrderResp.getUserId();
			int tradingId = placeOrderResp.getTradingId();
			int orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			BigDecimal payAmount = placeOrderResp.getPayAmount();

			new OrderCallService()
					.orderPayRequest(userId, payAmount, tradingId);

			new OrderCallService().acceptOrder(orderId, sellerId);

			changeEcommerceFlagCall(orderId, 0);

			deliveryTimeoutReq.setOrderId(orderId);

			deliveryTimeoutCall.setData(deliveryTimeoutReq);

			deliveryTimeoutCall.callService();

			Logger.verifyEquals("ok", deliveryTimeoutCall.getReturnData(),
					"验证Code");

			//数据库验证

			DefaultWapper database = new DefaultWapper();

			// 订单扩展表
			List<Map> ymtOrdersExt = database.selectOrderExtByOderId(orderId);

			Logger.verifyEquals(false,
					ymtOrdersExt.get(0).get("bHaveUploadedIdCard"), "验证身份证状态");
			Logger.verifyEquals(false,
					ymtOrdersExt.get(0).get("iXloboEcommerceFlag"),
					"验证iXloboEcommerceFlag");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_DeliveryTimeout_004() {
		Logger.start(true, "test");

		try {

			deliveryTimeoutReq.setOrderId(127133988);

			deliveryTimeoutCall.setData(deliveryTimeoutReq);

			deliveryTimeoutCall.callService();

			Logger.verifyEquals("ok", deliveryTimeoutCall.getReturnData(),
					"验证Code");

			Thread.sleep(3000);

			/*			//数据库验证

						DefaultWapper database = new DefaultWapper();

						// 订单扩展表
						List<Map> ymtOrdersExt = database
								.selectOrderExtByOderId(2138529335);

						Logger.verifyEquals(false,
								ymtOrdersExt.get(0).get("bHaveUploadedIdCard"), "验证身份证状态");
			*/
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}